package org.sensornet2010.co2sh.prototype;

import org.sensornet2010.co2sh.core.SensornetConfiguration;
import org.sensornet2010.co2sh.core.SensornetUtil;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

public class DummyCO2AppConf extends Activity implements OnClickListener{
	// TODO error handling (validation, persistence)
	
	public static final String CONFIGKEY_SERVER		= "SERVER";
	public static final String CONFIGKEY_SENSORID	= "SERVERID";
	public static final String CONFIGKEY_DURATION	= "DURATION";
	public static final String CONFIGKEY_INTERVAL	= "INTERVAL";
	public static final String CONFIGKEY_PROTOCOL	= "PROTOCOL";
	public static final String CONFIGKEY_FEED		= "FEED";
	public static final String CONFIGKEY_API		= "API";
	public static final String CONFIGKEY_DATASTREAM	= "DATASTREAM";
	public static final String CONFIGKEY_PACHUBE_ACTIVE = "PACHUBE_ACTIVE";
	
	// Called when the activity is first created. 
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.config);

		Button b_save = (Button) findViewById(R.id.b_save);
		populateConfiguration();
		b_save.setOnClickListener(this);
	}
	
	
	
	private void populateConfiguration(){
		try {
			// update GUI components with memory configuration
			EditText t_id = (EditText) findViewById(R.id.t_id);
			EditText t_server = (EditText) findViewById(R.id.t_server);
			EditText t_duration = (EditText) findViewById(R.id.t_duration);
			EditText t_interval = (EditText) findViewById(R.id.t_interval);
			EditText t_protocol = (EditText) findViewById(R.id.t_protocol);
			EditText t_feed = (EditText) findViewById(R.id.t_pachubeFeed);
			EditText t_api = (EditText) findViewById(R.id.t_pachubeApi);
			EditText t_fds = (EditText) findViewById(R.id.t_pachubeFDatastream);
			CheckBox t_pachubeCB = (CheckBox) findViewById(R.id.t_pachubeCB);
			

			// set gui data
			t_id.setText(SensornetConfiguration.SENSOR_ID);
			t_server.setText(SensornetConfiguration.SERVER_URL);
			t_protocol.setText(SensornetConfiguration.SENSUINO_VERSION);
			t_duration.setText(""+SensornetConfiguration.RECORDING_DURATION);
			t_interval.setText(""+SensornetConfiguration.RECORDING_INTERVAL);
			t_feed.setText(""+SensornetConfiguration.PACHUBE_FEED);
			t_api.setText(""+SensornetConfiguration.PACHUBE_API_KEY);
			t_fds.setText(""+SensornetConfiguration.PACHUBE_FEED_DATASTREAM);
			if (SensornetConfiguration.PACHUBE_ACTIVE) t_pachubeCB.setChecked(true);
			else t_pachubeCB.setChecked(false);
			
			
		} catch (Exception e) {
			SensornetUtil.debugException(e);
		}
		
	}
	
	
	
	@Override
	public void onClick(View v) {
		// gui components
		EditText t_id = (EditText) findViewById(R.id.t_id);
		EditText t_server = (EditText) findViewById(R.id.t_server);
		EditText t_duration = (EditText) findViewById(R.id.t_duration);
		EditText t_interval = (EditText) findViewById(R.id.t_interval);
		EditText t_protocol = (EditText) findViewById(R.id.t_protocol);
		EditText t_feed = (EditText) findViewById(R.id.t_pachubeFeed);
		EditText t_api = (EditText) findViewById(R.id.t_pachubeApi);
		EditText t_fds = (EditText) findViewById(R.id.t_pachubeFDatastream);
		CheckBox t_pachubeCB = (CheckBox) findViewById(R.id.t_pachubeCB);

		// get gui data
		String id = t_id.getText().toString();
		String server = t_server.getText().toString();
		String duration = t_duration.getText().toString();
		String interval = t_interval.getText().toString();
		String protocol = t_protocol.getText().toString();
		String feed = t_feed.getText().toString();
		String api = t_api.getText().toString();
		String datastream = t_fds.getText().toString();
		boolean pachubeActive = t_pachubeCB.isChecked();
		
		// validation and persistence
		SharedPreferences settings = getSharedPreferences(
				"sensornet.co2sh.profile", MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putString(CONFIGKEY_SENSORID, id);
		editor.putString(CONFIGKEY_SERVER, server);
		editor.putString(CONFIGKEY_PROTOCOL, protocol);
		editor.putLong(CONFIGKEY_DURATION, Long.parseLong(duration));
		editor.putInt(CONFIGKEY_INTERVAL, Integer.parseInt(interval));
		editor.putString(CONFIGKEY_API, feed);
		editor.putString(CONFIGKEY_API, api);
		editor.putString(CONFIGKEY_DATASTREAM, datastream);
		editor.putBoolean(CONFIGKEY_PACHUBE_ACTIVE, pachubeActive);
		editor.commit();
		
		// upload configuration in memory 
		SensornetConfiguration.SENSOR_ID = id;
		SensornetConfiguration.SERVER_URL = server;
		SensornetConfiguration.SENSUINO_VERSION = protocol;
		SensornetConfiguration.RECORDING_DURATION = Long.parseLong(duration);
		SensornetConfiguration.RECORDING_INTERVAL = Integer.parseInt(interval);
		SensornetConfiguration.PACHUBE_FEED = feed;
		SensornetConfiguration.PACHUBE_API_KEY = api;
		SensornetConfiguration.PACHUBE_FEED_DATASTREAM = datastream;
		
		CheckBox cb = (CheckBox) findViewById(R.id.t_pachubeCB);
		if(cb.isChecked()) SensornetConfiguration.PACHUBE_ACTIVE = true;
		else SensornetConfiguration.PACHUBE_ACTIVE = false;
		
		// back to main gui
		Intent intent = new Intent(DummyCO2AppConf.this,
				DummyCO2App.class);
		startActivityForResult(intent, 0x1337);
	}

}
